package com.javafactroy.kunlong.deptemp.testservice;

import com.javafactroy.kunlong.DbAgent;
import com.javafactroy.kunlong.base.model.ReturnPageInfo;
import com.javafactroy.kunlong.common.utils.CommonUtils;
import com.javafactroy.kunlong.domain.deptemp.agg.DeptTreeAgg;
import com.javafactroy.kunlong.domain.deptemp.factroy.DeptTreeAggFactroy;
import com.javafactroy.kunlong.domain.deptemp.repository.DeptTreeAggRepository;
import com.javafactroy.kunlong.domain.generalright.agg.UserRightAgg;
import com.javafactroy.kunlong.domain.generalright.repository.UserRightRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import com.javafactroy.kunlong.data.dto.VDeptApiDto;
import com.javafactroy.kunlong.data.service.VDeptApiService;
import com.github.jsonzou.jmockdata.JMockData;
import com.github.jsonzou.jmockdata.MockConfig;
import org.junit.jupiter.api.*;


/**
 * @author raymond.lei@163.com
 * @date 2023-12-04 15:06:33
 */

@ExtendWith(SpringExtension.class)
@Slf4j
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.MethodName.class)
@SpringBootTest(classes = {DbAgent.class}, webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class VDeptTestApiService {


    @Autowired
    DeptTreeAggFactroy deptTreeAggFactroy;
    @Autowired
    DeptTreeAggRepository deptTreeAggRepository;

    @Autowired
    VDeptApiService service;

    String pkeyField = "deptId";
    String pkeyProp = "pkey";

    public VDeptApiDto mock() {
        MockConfig mockConfig = new MockConfig()
                .doubleRange(1e-9d, 11112222.99999d)
                .floatRange(1e-8f, 11111112.99999f)
                .decimalScale(52).excludes("child", pkeyProp, "orderBys", "pageNum", "pageSize")
                .subConfig("status").intRange(0, 9).globalConfig();
        VDeptApiDto entity = JMockData.mock(VDeptApiDto.class, mockConfig);
        log.info("mock entity = {}", CommonUtils.toJSONString(entity));
        return entity;
    }

    @BeforeEach
    void setUp() {
        log.info("setUp{}", "...");
    }

    @AfterEach
    void tearDown() {
        log.info("tearDown{}", "...");
    }


    @Test
    public void test001_query() {
        // save();
        VDeptApiDto param = new VDeptApiDto();
        param.setPageSize(4).ge(pkeyField, 1);

        // param.like("mobileNo", "186");
        // param.notBetween("id", "1");
        // param.eq("opBy",-1);
        param.orderBy(pkeyField, "desc");

        ReturnPageInfo<VDeptApiDto> pageInfo = service.query(param);

        log.info("query pageInfo = {}", CommonUtils.toJSONString(pageInfo));

    }

    @Test
    public void test002_selectByPkey() {
        VDeptApiDto entity = service.selectByPkey(1l);
        log.info("selectByPkey entity = {}", entity);
        DeptTreeAgg agg = deptTreeAggRepository.load();
        log.info("agg={}", CommonUtils.toJSONString(agg));
    }

    @Autowired
    UserRightRepository userRightRepository;

    @Test
    public void test003_save() {

        VDeptApiDto entity = mock();
        //service.save(entity.setShopName("122").setContact("ddl"));

        //  service.save(entity);
        // log.info("save entity = {}", entity);
        DeptTreeAgg agg = deptTreeAggFactroy.loadRight(1L);
        log.info("{}", agg);

        UserRightAgg userRightAgg = userRightRepository.load(1L);
        log.info("{}", userRightAgg.getUserDepts());
    }

    VDeptApiDto save() {
        VDeptApiDto entity = mock();
        service.save(entity);
        return entity;
    }

}

